package ui.button
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[Event(name="change",type="flash.events.Event")]
	
	public class ToggleButtonView extends Sprite
	{
		
		public static const BUTTON_ON:String = "button_on";
		public static const BUTTON_OFF:String = "button_off";
		
		private var _state:String;
		
		public var onStateButton:Sprite;
		public var offSatebutton:Sprite;
		
		public function ToggleButtonView()
		{
			_state = BUTTON_OFF;
		
			onStateButton = getChildByName("onState") as Sprite;
			offSatebutton = getChildByName("offState") as Sprite;
			onStateButton.visible = false;
			
			addEventListener(MouseEvent.CLICK,buttonClickHandler);
		}
		
		private function buttonClickHandler(evt:Event):void
		{
			state = state == BUTTON_OFF ? BUTTON_ON : BUTTON_OFF;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function set state(_state:String):void
		{
			if(_state == state)
				return;
			
			this._state = _state;
			
			onStateButton.visible = state == BUTTON_ON;
			offSatebutton.visible = state == BUTTON_OFF;
		}
		
		public function get state():String
		{
			return _state;
		}
	}
}